package com.whut.controller;

import com.alibaba.fastjson.JSONObject;
import com.whut.model.Userinfo;
import com.whut.service.UserService;
import com.whut.utils.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @PostMapping("/getUser")
    public Object getUser(String id) {
        System.out.println(id);
        return userService.getUserById(Integer.parseInt(id));
    }


    @ResponseBody
    @PostMapping("/addUser")
    public Object getUser(Userinfo userinfo) {
        //System.out.println(id);
        System.out.println(userinfo);
        return userService.addUser(userinfo);
    }


    @ResponseBody
    @PostMapping("/getMultiParams")
    public Object getMultiParams(String params, List<MultipartFile> files) {
        String imgRootPath = "C:\\Users\\13983\\Desktop\\test";
        File dir = new File(imgRootPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        try {
            for (int i = 0; i < files.size(); i++) {
                String fileName = imgRootPath + "\\" + i + ".jpg";
                File file = new File(fileName);
                files.get(i).transferTo(file);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(params);
        return null;
    }


    @ResponseBody
    @PostMapping("/getGroupMembers")
    public Object getGroupMembers() {
        return userService.getAllMembers(1, 10);
    }

    @ResponseBody
    @PostMapping("/example1")
    public Object example1(int id) {
        //return userService.example(id);
        return null;
    }

    /*多参数查询*/
    @ResponseBody
    @PostMapping("/multiSelect")
    public Object multiSelect() {
        return userService.multiSelct(1, "M");
    }


    /**
    * 申请使用手机验证码注册，申请成功返回发送验证码成功的通知
    *@author fuzhenjie
    *@date 19.9.12 1:21(GMT)
    *@param userPhone 手机号码
    *@return String ["repetitive_phone","success","error"]
    */
    @ResponseBody
    @PostMapping("/registerWithSMS")
    public Object registerWithSMS(String userPhone) {
        return userService.registerWithSMS(userPhone);
    }

    /**
    * 申请使用手机验证码登录 申请成功返回发送验证码成功的通知
    *@author fuzhenjie
    *@date 19.9.12 1:20(GMT)
    *@param userPhone 手机号
    *@return String ["success",'error','no_this_user']
    */
    @ResponseBody
    @PostMapping("/loginWithSMS")
    public Object loginWithSMS(String userPhone) {
        return userService.loginWithSMS(userPhone);
    }

    /**
    * 通过手机号和验证码返回是否通过登录的校验信息
    *@author fuzhenjie
    *@date 19.9.12 1:19(GMT)
    *@param userPhone 手机号
    *@param v_code 验证码
    *@return java.lang.Object
    */
    @ResponseBody
    @PostMapping("/checkLoginWithSMS")
    public Object checkLoginWithSMS(String userPhone,String v_code){
        return userService.checkLoginWithSMS(userPhone,v_code);
    }

    /**
     * 通过手机号和验证码返回是否通过注册的校验信息
     *@author fuzhenjie
     *@date 19.9.12 1:19(GMT)
     *@param userPhone 手机号
     *@param v_code 验证码
     *@return java.lang.Object
     */
    @ResponseBody
    @PostMapping("/checkRegisterWithSMS")
    public Object checkRegisterWithSMS(String userPhone,String v_code,String userName,String password){
        return userService.checkRegisterWithSMSPC(userPhone,v_code,userName,password);
    }



    @ResponseBody
    @GetMapping("/encodingCode")
    public Object encodingCode(String unEncodingStr){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("encodedCode",MD5Encoder.getMD5(unEncodingStr));
        return jsonObject;
    }
}
